npm<template>
  <div>
    <el-button type="text" @click="dialogVisible = true">登录</el-button>

    <el-dialog
      title="登录"
      :visible.sync="dialogVisible"
      width="30%"
      center
      :before-close="handleClose">
      <span>
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="用户名">
            <el-input placeholder="请输入用户名" v-model="form.username"></el-input>
          </el-form-item>
          <el-form-item label="密码">
            <el-input placeholder="请输入密码" v-model="form.userpassword" show-password></el-input>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer" >
        <el-button type="primary" @click="login" class="submit">登 录</el-button>
        <el-button type="danger" class="submit"  @click="dialogVisible = true">注 册</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
  import axios from 'axios'
  axios.defaults.withCredentials = true
  export default {
    data() {
      return {
        form: {},
        dialogVisible: false
      }
    },
    methods: {
      login() {
        axios.post("http://127.0.0.1:8200/fancy-music-user/user/login", this.form).then(res => {
          alert(res.data)
          if (res.data != "没有该用户" && res.data != "密码错误") {
            this.$cookie.set("token", res.data)
            this.$router.push("/index")
          }
        })
      },
      handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {
          });
      }
    }
  }
</script>


